from MMU import *


def fetch(pc: bit_64, mem: memory, mmu: MMU):
    addr = mmu.to_phy_addr(pc.val, 0, mem)
    test = mem.data_8_port_out(addr)
    if test & 0b11 == 0b11:
        width = 32
        instr = mem.data_32_port_out(addr)
    else:
        width = 16
        instr = mem.data_16_port_out(addr)

    return instr, width


def IF(pc: bit_64, mem: memory, mmu: MMU):
    instr, width = fetch(pc, mem, mmu)
#    print("IF: instr = %s" % (hex(instr)))
    return instr, width, pc
